function convertImageToBase64(file) {
  return new Promise(resolve => {
    let reader = new FileReader();
    reader.addEventListener('load', function (e) {
      const base64Image = e.target.result
      reader = null
      resolve(base64Image)
    })
    reader.readAsDataURL(file);
  })
}
function compress(base64Image, MAX_W = 1024, MAX_H = 1024, quality = 0.8) {
  return new Promise(resolve => {
    const image = new Image()
    image.src = base64Image
    image.addEventListener('load', function (e) {
      let ratio
      let needCompress = false
      if (MAX_W < image.naturalWidth) {
        needCompress = true
        ratio = image.naturalWidth / MAX_W

        MAX_H = image.naturalHeight / ratio
      }
      if (MAX_H < image.naturalHeight) {
        needCompress = true
        ratio = image.naturalHeight / MAX_H
        MAX_W = image.naturalWidth / ratio
      }
      if (!needCompress) {
        MAX_W = image.naturalWidth
        MAX_H = image.naturalHeight
      }
      const canvas = document.createElement('canvas')
      canvas.setAttribute('id', "__compress__")
      canvas.width = MAX_W
      canvas.height = MAX_H
      const ctx = canvas.getContext('2d')
      ctx.clearRect(0, 0, MAX_W, MAX_H)
      ctx.drawImage(image, 0, 0, MAX_W, MAX_H)
      resolve(canvas.toDataURL('image/jpeg', quality))
    })
  })
}